package cz.mendelu.swi2.spravaProjektu;

import java.util.ArrayList;
import java.util.List;

/**
 * Umoznuje ukladani a ziskavani projektu z databaze.
 *
 */
public class ProjectManager {
	
	private List<Project> projects;
	private int projectsIdGenerator;
	
	public ProjectManager() {
		projects = new ArrayList<Project>();
		projectsIdGenerator = 1;
	}
	
	public void create(Project project) {
		project.setId(Long.valueOf((long) projectsIdGenerator++));
		projects.add(project);
	}
	
	public void edit(Project project) {
		for (Project p : projects) {
			if (p.getId() == project.getId()) {
				p.setName(project.getName());
				p.setDescription(project.getDescription());
				p.setState(project.getState());
				p.setRepository(project.getRepository());
			}
		}
	}
	
	public List<Project> find() {
		return projects;
	}
	
	public Project findById(Long id) {
		for (Project p : projects) {
			if (p.getId() == id) return p;
		}
		return null;
	}
	
	public boolean checkName(String name) {
		for (Project p : projects) {
			if (p.getName().equals(name)) return true;
		}
		return false;
	}
	
}
